<?php
/**
 * vanephp web framework
 * mysql 数据库驱动
 *
 * @author aray <ofree@qq.com>
 * @link http://www.vanephp.com/s
 * @copyright Copyright &copy; 2010 aray
 * @license http://www.vanephp.com/license
 * @package db
 */

/**
 * mysql数据库驱动
 * @author aray
 *
 */
class vane_db_mysql {
	private $queryLog = array ();
	private $conn = null;
	private $dbConfig = array ();
	private $dbSchema = '`';
	
	public function vane_db_mysql ( $args = array() ) {
		$this->dbConfig = $args;
	}
	
	/**
	 * 获取查询日志
	 * @return array
	 */
	public function getQuerylog ( ) {
		return $this->queryLog;
	}
	
	/**
	 * 获取数据库存SCHEMA
	 */
	public function getDbSchema ( ) {
		return $this->dbSchema;
	}
	
	/**
	 * 关闭数据连接
	 */
	public function close ( ) {
		if ( $this->conn )
			mysql_close ( $this->conn );
		$this->conn = null;
	}
	
	/**
	 * 连接数据库
	 * @param int $index	dsn配置的数组下标
	 */
	public function connect ( $index = 0 ) {
		if ( ! $this->conn ) {
			if ( isset ( $this->dbConfig['host'] ) && isset ( $this->dbConfig['user'] ) && isset ( $this->dbConfig['pass'] ) ) {
				$port = isset ( $this->dbConfig['port'] ) ? $this->dbConfig['port'] : 3306;
				$this->conn = mysql_connect ( $this->dbConfig['host'] . ':' . $port, $this->dbConfig['user'], $this->dbConfig['pass'], null, isset ( $this->dbConfig['option'] ) ? $this->dbConfig['option'] : null );
				if ( $this->conn ) {
					$db = mysql_select_db ( $this->dbConfig['name'], $this->conn );
					if ( $db ) {
						static $firstRun = false;
						if ( $firstRun == false ) {
							$this->setCharset ( );
							$firstRun = true;
						}
					} else {
						trigger_error ( '无法选择数据库', E_USER_ERROR );
					}
				} else {
					trigger_error ( '无法连接数据库', E_USER_ERROR );
				}
			} else {
				trigger_error ( '没有配置数据库的基础信息，无法连接数据库', E_USER_ERROR );
			}
		}
		return $this;
	}
	
	/**
	 * 设置字符集
	 */
	public function setCharset ( ) {
		$setNames = sprintf ( 'SET NAMES %s%s%s', $this->dbSchema, isset ( $this->dbConfig['charset'] ) ? $this->dbConfig['charset'] : 'UTF8', $this->dbSchema );
		$this->query ( $setNames );
	}
	
	/**
	 * 查询SQL
	 * @param string $sql		SQL语句
	 * @return resource
	 */
	public function query ( $sql ) {
		if ( empty ( $sql ) )
			return;
		$query = null;
		if ( $this->conn ) {
			$query = mysql_query ( $sql, $this->conn );
			
			if ( $query ) {
				$this->queryLog[] = $sql;
				log::set ( 'SQL', $sql );
			} else {
				$err = "查询SQL {$sql} 错误," . mysql_error ( $this->conn );
				log::set ( 'ERR', $err );
				mysql_query ( 'SET @VANE_QUERY_ERROR = true', $this->conn );
				trigger_error ( $err, E_USER_ERROR );
			}
		} else {
			trigger_error ( '没有数据库连接，查询失败', E_USER_ERROR );
		}
		return $query;
	}
	
	/**
	 * 查询SQL并返回多条结果
	 * @param string $sql		SQL语句
	 * @return array
	 */
	public function getAll ( $sql ) {
		$query = $this->query ( $sql );
		$rowset = array ();
		$next = true;
		while ( $next && $query ) {
			$row = mysql_fetch_array ( $query, MYSQL_ASSOC );
			if ( $row ) {
				$rowset[] = $row;
				$next = true;
			} else {
				$next = false;
			}
		}
		mysql_free_result ( $query );
		return $rowset;
	}
	
	/**
	 * 查询SQL并返回一条结果		SQL语句
	 * @param string $sql
	 * @return array
	 */
	public function getOne ( $sql ) {
		$sql = ltrim ( $sql );
		if ( strtoupper ( substr ( $sql, 0, '6' ) ) == 'SELECT' && stripos ( $sql, 'limit' ) === false ) {
			$sql .= ' LIMIT 1';
		}
		$row = array ();
		$rowset = $this->getAll ( $sql );
		return array_shift ( $rowset );
	}
	
	/**
	 * 查询SQL并返回一列结果
	 * @param string $sql		SQL语句
	 * @return mixed
	 */
	public function getCol ( $sql ) {
		$row = null;
		$one = $this->getOne ( $sql );
		$col = array_shift ( $one );
		return $col;
	}
	
	/**
	 * 获取上次查询所影响的行数
	 * @return int
	 */
	public function getAffected ( ) {
		return $this->getCol ( 'SELECT ROW_COUNT()' );
	}
	
	/**
	 * 获取上次INSERT操作所产生的自动编号ID
	 * @return int
	 */
	public function getLastInsertId ( ) {
		return $this->getCol ( 'SELECT LAST_INSERT_ID()' );
	}
	
	/**
	 * 获取表字段信息
	 * @param $table		数据表名称
	 */
	function getTableMeta ( $table ) {
		$sql = 'SHOW FIELDS FROM ' . $table;
		$metas = $this->getAll ( $sql );
		return $metas;
	}
}